package com.fighthouse.utils
{
	import com.fighthouse.entity.Banner;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.utils.clearInterval;
	import flash.utils.setTimeout;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HBox;
	import mx.controls.ProgressBar;
	import mx.controls.SWFLoader;
	import mx.core.UIComponent;

	public class MultiBanner extends Canvas
	{
		private var item:LoaderItem;
		
		private var itens:ArrayCollection;
		private var itensManager:HBox;
		
		private var loader:SWFLoader;
		private var loaderBar:ProgressBar;
		
		private var interval:int;
		
		public function MultiBanner()
		{
			super();
			
			loaderBar = new ProgressBar();
			loaderBar.source = loader;
			loaderBar.setStyle("horizontalCenter",0);
			loaderBar.setStyle("verticalCenter", 0);
			addChild(loaderBar);
			
			loader = new SWFLoader();
			loader.percentWidth = 100;
			loader.percentHeight = 100;
			loader.scaleContent = true;
			loader.maintainAspectRatio = false;
			loader.addEventListener(Event.COMPLETE,onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
			addChild(loader);
			
			itensManager = new HBox();
			itensManager.setStyle("bottom",19);
			itensManager.setStyle("horizontalCenter",0);
			itensManager.setStyle("horizontalGap",22);
			itensManager.width = 100;
			itensManager.setStyle("backgroundColor",0x000000);
			addChild(itensManager);
		}
				
		public function set banners(value:ArrayCollection):void
		{
			if(value)
			{
				itens = value;
				for(var i:int; i < itens.length; i++)
				{
					var o:LoaderItem = new LoaderItem(Banner(itens.getItemAt(i)));
					o.target = this;
					itensManager.addChild(o);
					if(i == 0)
						selectedItem = o;
				}
			}
		}		
		
		public function set selectedItem(value:LoaderItem):void
		{
			if(item)
			item.selected = false;
			
			item = value;
			item.selected = true;
			
			if(loader.content && loader.loaderInfo.contentType == "application/x-shockwave-flash")
			{
				loader.content.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			}
			loader.source = "assets/banners/" + item.banner.arquivo;
		}
		
		public function get selectedItem():LoaderItem
		{
			return item;
		}
		
		private function onError(event:IOErrorEvent):void
		{
			trace(event);
		}
		
		private function onComplete(event:Event):void
		{
			var swfLoader:SWFLoader = SWFLoader(event.target);
			switch(swfLoader.content.toString())
			{
				case "[object MainTimeline]":
					MovieClip(swfLoader.content).gotoAndPlay(0);
					MovieClip(swfLoader.content).addEventListener(Event.ENTER_FRAME, onEnterFrame);
				break;
				default:
					clearInterval(interval);
					interval = setTimeout(selectNextItem,3000);
				break;
			}
		}
		
		private function onEnterFrame(event:Event):void
		{
			var m:MovieClip = MovieClip(event.target);
			if(m.currentFrame == m.totalFrames)
			{
				m.stop();
				event.target.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				selectNextItem();
			}
		}
		
		private function selectNextItem():void
		{
			for(var i:int; i < itensManager.numChildren; i++)
			{
				if(itensManager.getChildAt(i) == selectedItem)
				{
					if((i + 1) < itensManager.numChildren)
					{
						selectedItem = itensManager.getChildAt(i + 1) as LoaderItem;
						break;
					}	
					else
						selectedItem = itensManager.getChildAt(0) as LoaderItem;
				}
			}
		}
	}
}


	import mx.core.UIComponent
	import flash.text.TextFormat;
	import flash.text.TextField; 
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.display.GradientType;
	import flash.filters.DropShadowFilter;
	import flash.text.TextFormatAlign;
	import flash.text.AntiAliasType;
	import flash.text.TextFieldAutoSize;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.events.ProgressEvent;
	import flash.events.Event;
	import com.fighthouse.entity.Banner;
	import mx.events.TweenEvent;
	import mx.effects.Tween;
	import com.fighthouse.utils.MultiBanner;
	import mx.controls.TextArea;
	
	class LoaderItem extends UIComponent
	{
		private var labelTF:TextFormat;
		private var label:TextField;
		private var fundo:Sprite;
		private var selection:Sprite;
		private var mascara:Sprite;
				
		//private var twSelection:Tween;
		
		public var banner:Banner;
		private var isSelected:Boolean = false;
		
		public var target:MultiBanner;
		
		public function LoaderItem(banner:Banner)
		{
			this.banner = banner;
			
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(21,19,Math.PI / 2,0,0);
			
			fundo = new Sprite();
			fundo.graphics.beginGradientFill(GradientType.LINEAR,
						[0xd5dce5, 0x93a6be],[1,1],[0,255],
						matrix);
			fundo.graphics.drawRect(0,0,21,19);
			fundo.graphics.endFill();
			fundo.mouseEnabled = false;
			addChild(fundo);
			
			matrix = new Matrix();
			matrix.createGradientBox(21,19,Math.PI / 2,0,0);
			
			selection = new Sprite();
			selection.graphics.beginGradientFill(GradientType.LINEAR,
						[0xa6b5c9, 0x344751],[1,1],[0,255],
						matrix);
			selection.graphics.drawRect(0,0,21,19);
			selection.graphics.endFill();
			selection.graphics.beginFill(0xFFFFFF,.2);
			selection.graphics.drawRect(0,0,21,11);
			selection.graphics.endFill();
			selection.mouseEnabled = false;
			selection.filters = [new DropShadowFilter(0,45,0,.97,4,4,1,3,true)];
			selection.visible = false;
			addChild(selection)
			
			mascara = new Sprite();
			mascara.graphics.beginFill(0,.2);
			mascara.graphics.drawRect(0,0,21,19);
			mascara.graphics.endFill();
			addChild(mascara);
			
			selection.mask = mascara;
			
			//twSelection = new Tween(mascara, 0,0,1,30, onMotionChange,onMotionFinish);
			
			
			labelTF = new TextFormat();
			labelTF.font = "Futura Lt BT";
			labelTF.size = 13;
			labelTF.align = TextFormatAlign.CENTER;
			labelTF.color = 0x000000;
			labelTF.leftMargin = 0;
			labelTF.rightMargin = 0;
			labelTF.leading = 0;
			labelTF.letterSpacing = 0;
			
			label = new TextField();
			label.embedFonts = true;
			label.antiAliasType = AntiAliasType.ADVANCED;
			label.autoSize = TextFieldAutoSize.LEFT;
			label.height = 19;
			label.defaultTextFormat = labelTF;
			label.text = banner.id.toString();
			label.border = false;
			label.mouseEnabled = false;
			label.x = fundo.width/2 - label.width/2;
			addChild(label); 
			
			this.buttonMode = true;
			this.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			this.addEventListener(MouseEvent.CLICK, clickHandler);
		}
		
		public function set selected(value:Boolean):void
		{
			if(isSelected != value)
			{
				isSelected = value;
				if(isSelected)
				{
					selection.visible = true;
					//label.textColor = 0xFFFFFF;
					//twSelection.reverse();
					//twSelection.continueTo(selection.height, 30);
				}
				else
				{
					selection.visible = false;
					//label.textColor = 0;
					//twSelection.rewind();
				}
			}
		}
		
		public function get selected():Boolean
		{
			return isSelected;
		}
		
		private function clickHandler(event:MouseEvent):void
		{
			if(!selected)
				target.selectedItem = this;
		}
		
		private function mouseOverHandler(event:MouseEvent):void
		{
			if(!selected)
			{
				//label.textColor = 0xFFFFFF;
			}
		}
		
		private function mouseOutHandler(event:MouseEvent):void
		{
			if(!selected)
			{
				//label.textColor = 0;
			}
		}
		
		private function onMotionChange(event:TweenEvent):void
		{
			//event.target.y = selection.height - event.;
		}
		private function onMotionFinish(event:TweenEvent):void
		{
			
		}
	}